<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Response;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function success($data = [], $message = '成功', $code = 200, $type_id = 2)
    {
        return $this->apiReturn(true, $code, $message, $data, $type_id);
    }

    public function failure($message = '失败', $data = [], $code = 500, $type_id = 2)
    {
        return $this->apiReturn(false, $code, $message, $data, $type_id);
    }

    /**
     * @param bool $status
     * @param int $code
     * @param string $message
     * @param array $data
     * @param int $type_id {1：空数组返回空数组，2：空数组返回空对象}
     * @return \Illuminate\Http\JsonResponse
     */
    public function apiReturn($status = true, $code = 200, $message = '成功', $data = [], $type_id = 2)
    {
        if (empty($data)) {
            if ($type_id == 2) {
                $data = new \stdClass(); //空数组转化为空对象（兼容安卓）
            } else {
                $data = [];
            }
        }

        $box['status'] = $status;
        $box['code'] = $code;
        $box['message'] = $message;
        $box['data'] = $data;

        return Response::json($box);
    }
}
